<template>
  <div>
    <vxe-toolbar id="toolbarId" :setting="{storage: true}" export :refresh="{query: findList}">
      <template v-slot:buttons>
        <el-button @click="findList">查询</el-button>
        <el-button v-if="!parentData.isDetail" @click="funcAdd">新增</el-button>
      </template>
    </vxe-toolbar>
    <vxe-table
      ref="xTable"
      border
      show-footer
      highlight-hover-row
      row-id="id"
      class="mytable-footer"
      height="700"
      :data="tableData"
      remote-sort
      @sort-change="sortList"
      :sort-config="{ remote: true }"
      export-config
    >
      <vxe-table-column type="checkbox" width="60"  />
      <vxe-table-column>
        <vxe-table-column field="serialNum" title="合同编号" min-width="100" sortable/>
        <template v-slot:header>
          <el-input v-model="listQuery.serialNum" placeholder="请输入合同编号" clearable/>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="createdBy" title="创建人" min-width="100" sortable/>
        <template v-slot:header>
          <el-input v-model="listQuery.createdBy" placeholder="请输入创建人" clearable/>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="signTime" title="签订时间" min-width="320" sortable/>
        <!--template v-slot:header>
          <el-input v-model="listQuery.signTime" placeholder="请输入签订时间" clearable/>
        </template>-->
        <template v-slot:header>
          <el-date-picker
            v-model="signTimeRange"
            type="daterange"
            value-format="yyyy-MM-dd"
            :picker-options="pickerOptions"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="changeSignDate"
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="gmtCreated"
          title="创建时间"
          min-width="320"
          sortable
        />
        <template v-slot:header>
          <el-date-picker
            v-model="gmtCreatedRange"
            type="daterange"
            value-format="yyyy-MM-dd"
            :picker-options="pickerOptions"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="changeCreatedDate"
          />
        </template>
      </vxe-table-column>
      <vxe-table-column v-if="false">
        <vxe-table-column field="fileUrl" title="附件地址" min-width="100" sortable/>
        <template v-slot:header>
          <el-input v-model="listQuery.fileUrl" placeholder="请输入附件地址" clearable/>
        </template>
      </vxe-table-column>
      <vxe-table-column v-if="false">
        <vxe-table-column field="code" title="UUID" min-width="100" sortable/>
        <template v-slot:header>
          <el-input v-model="listQuery.code" placeholder="请输入UUID" clearable/>
        </template>
      </vxe-table-column>
      <vxe-table-column v-if="false">
        <vxe-table-column field="branchCode" title="机构code" min-width="100" sortable/>
        <template v-slot:header>
          <el-input v-model="listQuery.branchCode" placeholder="请输入机构code" clearable/>
        </template>
      </vxe-table-column>
      <vxe-table-column v-if="false">
        <vxe-table-column field="partyCode" title="用户编码" min-width="100" sortable/>
        <template v-slot:header>
          <el-input v-model="listQuery.partyCode" placeholder="请输入用户编码" clearable/>
        </template>
      </vxe-table-column>
      <vxe-table-column title="操作"  field="action" fixed="right">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="详情" placement="top">
            <vxe-button type="text" @click="funcDetail(scope.row.code)">
              <i class="el-icon-document"/>
            </vxe-button>
          </el-tooltip>
          <el-tooltip v-if="!parentData.isDetail" class="item" effect="dark" content="编辑" placement="top">
            <vxe-button type="text" @click="funcEdit(scope.row.code)">
              <i class="el-icon-edit"/>
            </vxe-button>
          </el-tooltip>
          <el-tooltip v-if="!parentData.isDetail" class="item" effect="dark" content="删除" placement="top">
            <vxe-button type="text" @click="funcDelete(scope.row.code)">
              <i class="el-icon-delete"/>
            </vxe-button>
          </el-tooltip>
        </template>
      </vxe-table-column>
    </vxe-table>
    <vxe-pager
      :loading="loading"
      :current-page.sync="listQuery.pageNum"
      :page-size.sync="listQuery.pageSize"
      :total="listQuery.total"
      :layouts="['PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total']"
      @page-change="handlePageChange"
    />

    <vxe-modal v-model="modelAddFlag" title="新增" width="500" height="350" resize>
      <compAdd @fatherAddMethod="fatherAdd" @fatherCancelMethod="fatherCancel"/>
    </vxe-modal>
    <vxe-modal v-model="modelEditFlag" title="编辑" width="500" height="350" resize>
      <compEdit ref="editRef" @fatherEditMethod="fatherEdit" @fatherCancelMethod="fatherCancel"/>
    </vxe-modal>
    <vxe-modal v-model="modelDetailFlag" title="详情" width="500" height="300" resize>
      <compDetail ref="detailRef"/>
    </vxe-modal>

  </div>
</template>
<script>
  import compAdd from './components/shipmentContractAdd.vue'
  import compEdit from './components/shipmentContractEdit.vue'
  import compDetail from './components/shipmentContractDetail.vue'
  import request from '@/utils/request'
  import { getShipmentContrctList, delShipmentContract } from '@/api/shipment'

  export default {
    name: 'shipmentContractList',
    components: {
      compAdd,
      compEdit,
      compDetail
    },
    props: {
      parentData: {
        type: Object,
        default() {
          return {
            isDetail: false,
            areaOptions: [],
            goodsTypeList: [],
            typeOptions: [],
            vehicleDictList: [],
            bankOptions: [],
            receiverUserCodeOptions: []
          };
        }
      }
    },
    data() {
      return {
        message: '',
        loading: false,
        tableData: [],
        signTimeRange: '',
        gmtCreatedRange: '',
        modelAddFlag: false,
        modelEditFlag: false,
        modelDetailFlag: false,
        copy: false,
        listQuery: {
          code: '',
          branchCode: '',
          partyCode: '',
          serialNum: '',
          signTimeStart: '',
          signTimeEnd: '',
          gmtCreatedStart: '',
          gmtCreatedEnd: '',
          createdBy: '',
          fileUrl: '',
          pageNum: 1,
          pageSize: 10,
          sortField: '',
          sortType: '',
          total: 0
        },
        pickerOptions: {
          shortcuts: [
            {
              text: '最近一周',
              onClick(picker) {
                const end = new Date()
                const start = new Date()
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
                picker.$emit('pick', [start, end])
              }
            },
            {
              text: '最近一个月',
              onClick(picker) {
                const end = new Date()
                const start = new Date()
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
                picker.$emit('pick', [start, end])
              }
            },
            {
              text: '最近三个月',
              onClick(picker) {
                const end = new Date()
                const start = new Date()
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
                picker.$emit('pick', [start, end])
              }
            }
          ]
        }
      }
    },
    created() {
      this.listQuery.partyCode = this.$route.query.partyCode
        ? this.$route.query.partyCode
        : '';
      //console.log("list"+this.listQuery.partyCode);
      this.findList();
    },
    methods: {
      findList() {
        this.loading = true;
        return new Promise(resolve => {
          getShipmentContrctList(this.listQuery).then(response => {
            this.tableData = response.data.records;
            this.listQuery.total = response.data.total;
            this.loading = false;
            resolve();
          })
        })
      },
      //选择创建时间
      changeCreatedDate(value) {
        if (value) {
          this.listQuery.gmtCreatedStart = value[0] + ' 00:00:00'
          this.listQuery.gmtCreatedEnd = value[1] + ' 23:59:59'
        } else {
          this.listQuery.gmtCreatedStart = ''
          this.listQuery.gmtCreatedEnd = ''
        }
      },
      //选择签订时间
      changeSignDate(value) {
        if (value) {
          this.listQuery.signTimeStart = value[0] + ' 00:00:00'
          this.listQuery.signTimeEnd = value[1] + ' 23:59:59'
        } else {
          this.listQuery.signTimeStart = ''
          this.listQuery.signTimeEnd = ''
        }
      },
      // 排序
      sortList({ column, property, order }) {
        if (order) {
          this.listQuery.sortField = property
          this.listQuery.sortType = order
        } else {
          this.listQuery.sortField = ''
        }
        this.findList()
      },
      // 添加方法
      funcAdd() {
        this.modelAddFlag = true
      },
      // 修改方法
      funcEdit(code) {
        this.$refs.editRef.getInfo(code)
        this.modelEditFlag = true
      },
      // 详情方法
      funcDetail(code) {
        this.$refs.detailRef.getInfo(code)
        this.modelDetailFlag = true
      },
      // 删除方法
      funcDelete(codes) {
        this.$confirm('确认删除吗？')
          .then(_ => {
            delShipmentContract({ codes }).then(response => {
              this.$message({ message: response.msg, type: 'success' })
              this.findList()
            })
          })
          .catch(_ => {
          })
      },
      // 增加组件的父方法
      fatherAdd() {
        this.modelAddFlag = false
        this.findList()
      },
      // 编辑组件的父方法
      fatherEdit() {
        this.modelEditFlag = false
        this.findList()
      },
      // 公共取消
      fatherCancel(modelFlag) {
        switch (modelFlag) {
          case 'add':
            this.modelAddFlag = false
            break
          case 'edit':
            this.modelEditFlag = false
            break
          case 'detail':
            this.modelDetailFlag = false
            break
          default:
        }
      },
      // 分页handle
      handlePageChange({ currentPage, pageSize }) {
        this.listQuery.pageNum = currentPage
        this.listQuery.pageSize = pageSize
        this.findList()
      }
    }
  }
</script>
